home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / pt-misc.h < prev    next >
C/C++ Source or Header  |  1997-03-07  |  9KB  |  451 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_tree_misc_h)
  24. #define octave_tree_misc_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. class ostream;
  31.  
  32. class octave_value_list;
  33. class octave_value;
  34. class tree_command;
  35. class tree_expression;
  36. class tree_simple_assignment_expression;
  37. class tree_index_expression;
  38. class tree_identifier;
  39. class symbol_record;
  40. class symbol_table;
  41.  
  42. class tree_statement;
  43. class tree_statement_list;
  44. class tree_argument_list;
  45. class tree_parameter_list;
  46. class tree_return_list;
  47. class tree_va_return_list;
  48. class tree_global;
  49. class tree_global_init_list;
  50. class tree_if_clause;
  51. class tree_if_command_list;
  52. class tree_switch_case;
  53. class tree_switch_case_list;
  54.  
  55. class tree_walker;
  56.  
  57. #include <SLList.h>
  58.  
  59. #include "pt-base.h"
  60.  
  61. // A statement is either a command to execute or an expression to
  62. // evaluate.
  63.  
  64. class
  65. tree_statement
  66. {
  67. friend class tree_statement_list;
  68.  
  69. public:
  70.  
  71.   tree_statement (void)
  72.     : cmd (0), expr (0), print_flag (true) { }
  73.  
  74.   tree_statement (tree_command *c)
  75.     : cmd (c), expr (0), print_flag (true) { }
  76.  
  77.   tree_statement (tree_expression *e)
  78.     : cmd (0), expr (e), print_flag (true) { }
  79.  
  80.   ~tree_statement (void);
  81.  
  82.   void set_print_flag (bool print)
  83.     { print_flag = print; }
  84.  
  85.   bool is_command (void)
  86.     { return cmd != 0; }
  87.  
  88.   bool is_expression (void)
  89.     { return expr != 0; }
  90.  
  91.   int line (void);
  92.   int column (void);
  93.  
  94.   void maybe_echo_code (bool);
  95.  
  96.   bool print_result (void) { return print_flag; }
  97.  
  98.   tree_command *command (void) { return cmd; }
  99.  
  100.   tree_expression *expression (void) { return expr; }
  101.  
  102.   void accept (tree_walker& tw);
  103.  
  104. private:
  105.  
  106.   // Only one of cmd or expr can be valid at once.
  107.  
  108.   // Command to execute.
  109.   tree_command *cmd;
  110.  
  111.   // Expression to evaluate.
  112.   tree_expression *expr;
  113.  
  114.   // Print result of eval for this command?
  115.   bool print_flag;
  116. };
  117.  
  118. // A list of statements to evaluate.
  119.  
  120. class
  121. tree_statement_list : public SLList<tree_statement *>
  122. {
  123. public:
  124.  
  125.   tree_statement_list (void)
  126.     : SLList<tree_statement *> (), function_body (false) { }
  127.  
  128.   tree_statement_list (tree_statement *s)
  129.     : SLList<tree_statement *> (), function_body (false) { append (s); }
  130.  
  131.   ~tree_statement_list (void)
  132.     {
  133.       while (! empty ())
  134.     {
  135.       tree_statement *t = remove_front ();
  136.       delete t;
  137.     }
  138.     }
  139.  
  140.   void mark_as_function_body (void) { function_body = true; }
  141.  
  142.   octave_value eval (bool print);
  143.  
  144.   octave_value_list eval (bool print, int nargout);
  145.  
  146.   void accept (tree_walker& tw);
  147.  
  148. private:
  149.  
  150.   // Does this list of statements make up the body of a function?
  151.   bool function_body;
  152. };
  153.  
  154. // Argument lists.  Used to hold the list of expressions that are the
  155. // arguments in a function call or index expression.
  156.  
  157. class
  158. tree_argument_list : public SLList<tree_expression *>
  159. {
  160. public:
  161.  
  162.   tree_argument_list (void)
  163.     : SLList<tree_expression *> () { }
  164.  
  165.   tree_argument_list (tree_expression *t)
  166.     : SLList<tree_expression *> () { append (t); }
  167.  
  168.   ~tree_argument_list (void)
  169.     {
  170.       while (! empty ())
  171.     {
  172.       tree_expression *t = remove_front ();
  173.       delete t;
  174.     }
  175.     }
  176.  
  177.   octave_value_list convert_to_const_vector (void);
  178.  
  179.   void accept (tree_walker& tw);
  180. };
  181.  
  182. // Parameter lists.  Used to hold the list of input and output
  183. // parameters in a function definition.  Elements are identifiers
  184. // only.
  185.  
  186. class
  187. tree_parameter_list : public SLList<tree_identifier *>
  188. {
  189. public:
  190.  
  191.   tree_parameter_list (void)
  192.     : SLList<tree_identifier *> (), marked_for_varargs (0) { }
  193.  
  194.   tree_parameter_list (tree_identifier *t)
  195.     : SLList<tree_identifier *> (), marked_for_varargs (0) { append (t); }
  196.  
  197.   ~tree_parameter_list (void);
  198.  
  199.   void mark_as_formal_parameters (void);
  200.  
  201.   void mark_varargs (void)
  202.     { marked_for_varargs = 1; }
  203.  
  204.   bool takes_varargs (void) const
  205.     { return marked_for_varargs != 0; }
  206.  
  207.   void mark_varargs_only (void)
  208.     { marked_for_varargs = -1; }
  209.  
  210.   bool varargs_only (void)
  211.     { return (marked_for_varargs < 0); }
  212.  
  213.   void initialize_undefined_elements (octave_value& val);
  214.  
  215.   void define_from_arg_vector (const octave_value_list& args);
  216.  
  217.   bool is_defined (void);
  218.  
  219.   octave_value_list convert_to_const_vector (tree_va_return_list *vr_list);
  220.  
  221.   void accept (tree_walker& tw);
  222.  
  223. private:
  224.  
  225.   int marked_for_varargs;
  226. };
  227.  
  228. // Return lists.  Used to hold the right hand sides of multiple
  229. // assignment expressions.
  230.  
  231. class
  232. tree_return_list : public SLList<tree_index_expression *>
  233. {
  234. public:
  235.  
  236.   tree_return_list (void)
  237.     : SLList<tree_index_expression *> () { }
  238.  
  239.   tree_return_list (tree_index_expression *t)
  240.     : SLList<tree_index_expression *> () { append (t); }
  241.  
  242.   ~tree_return_list (void);
  243.  
  244.   void accept (tree_walker& tw);
  245. };
  246.  
  247. class
  248. tree_va_return_list : public SLList<octave_value>
  249. {
  250. public:
  251.  
  252.   tree_va_return_list (void) : SLList<octave_value> () { }
  253.  
  254.   ~tree_va_return_list (void) { }
  255. };
  256.  
  257. // List of expressions that make up a global statement.
  258.  
  259. class
  260. tree_global
  261. {
  262. public:
  263.  
  264.   tree_global (void)
  265.     : id (0), ass_expr (0) { }
  266.  
  267.   tree_global (tree_identifier *i)
  268.     : id (i), ass_expr (0) { }
  269.  
  270.   tree_global (tree_simple_assignment_expression *ass)
  271.     : id (0), ass_expr (ass) { }
  272.  
  273.   ~tree_global (void);
  274.  
  275.   void eval (void);
  276.  
  277.   tree_identifier *ident (void) { return id; }
  278.  
  279.   tree_simple_assignment_expression *assign_expr (void) { return ass_expr; }
  280.  
  281.   void accept (tree_walker& tw);
  282.  
  283. private:
  284.  
  285.   // Only one of id or ass_expr can be valid at once.
  286.  
  287.   // An identifier to make global.
  288.   tree_identifier *id;
  289.  
  290.   // An assignemnt expression.  Valid only if the left hand side of
  291.   // the assignment is a simple identifier.
  292.   tree_simple_assignment_expression *ass_expr;
  293. };
  294.  
  295. class
  296. tree_global_init_list : public SLList<tree_global *>
  297. {
  298. public:
  299.  
  300.   tree_global_init_list (void)
  301.     : SLList<tree_global *> () { }
  302.  
  303.   tree_global_init_list (tree_global *t)
  304.     : SLList<tree_global *> () { append (t); }
  305.  
  306.   ~tree_global_init_list (void)
  307.     {
  308.       while (! empty ())
  309.     {
  310.       tree_global *t = remove_front ();
  311.       delete t;
  312.     }
  313.     }
  314.  
  315.   void eval (void);
  316.  
  317.   void accept (tree_walker& tw);
  318. };
  319.  
  320. class
  321. tree_if_clause
  322. {
  323. public:
  324.  
  325.   tree_if_clause (void) : expr (0), list (0) { }
  326.  
  327.   tree_if_clause (tree_statement_list *l)
  328.     : expr (0), list (l) { }
  329.  
  330.   tree_if_clause (tree_expression *e, tree_statement_list *l)
  331.     : expr (e), list (l) { }
  332.  
  333.   ~tree_if_clause (void);
  334.  
  335.   bool is_else_clause (void)
  336.     { return ! expr; }
  337.  
  338.   int eval (void);
  339.  
  340.   tree_expression *condition (void) { return expr; }
  341.  
  342.   tree_statement_list *commands (void) { return list; }
  343.  
  344.   void accept (tree_walker& tw);
  345.  
  346. private:
  347.  
  348.   // The condition to test.
  349.   tree_expression *expr;
  350.  
  351.   // The list of statements to evaluate if expr is true.
  352.   tree_statement_list *list;
  353. };
  354.  
  355. class
  356. tree_if_command_list : public SLList<tree_if_clause *>
  357. {
  358. public:
  359.  
  360.   tree_if_command_list (void)
  361.     : SLList<tree_if_clause *> () { }
  362.  
  363.   tree_if_command_list (tree_if_clause *t)
  364.     : SLList<tree_if_clause *> () { append (t); }
  365.  
  366.   ~tree_if_command_list (void)
  367.     {
  368.       while (! empty ())
  369.     {
  370.       tree_if_clause *t = remove_front ();
  371.       delete t;
  372.     }
  373.     }
  374.  
  375.   void eval (void);
  376.  
  377.   void accept (tree_walker& tw);
  378. };
  379.  
  380. class
  381. tree_switch_case
  382. {
  383. public:
  384.  
  385.   tree_switch_case (void) : label (0), list (0) { }
  386.  
  387.   tree_switch_case (tree_statement_list *l)
  388.     : label (0), list (l) { }
  389.  
  390.   tree_switch_case (tree_expression *e, tree_statement_list *l)
  391.     : label (e), list (l) { }
  392.  
  393.   ~tree_switch_case (void);
  394.  
  395.   bool is_default_case (void)
  396.     { return ! label; }
  397.  
  398.   bool label_matches (const octave_value& val);
  399.  
  400.   int eval (const octave_value& val);
  401.  
  402.   void eval_error (void);
  403.  
  404.   tree_expression *case_label (void) { return label; }
  405.  
  406.   tree_statement_list *commands (void) { return list; }
  407.  
  408.   void accept (tree_walker& tw);
  409.  
  410. private:
  411.  
  412.   // The case label.
  413.   tree_expression *label;
  414.  
  415.   // The list of statements to evaluate if the label matches.
  416.   tree_statement_list *list;
  417. };
  418.  
  419. class
  420. tree_switch_case_list : public SLList<tree_switch_case *>
  421. {
  422. public:
  423.  
  424.   tree_switch_case_list (void)
  425.     : SLList<tree_switch_case *> () { }
  426.  
  427.   tree_switch_case_list (tree_switch_case *t)
  428.     : SLList<tree_switch_case *> () { append (t); }
  429.  
  430.   ~tree_switch_case_list (void)
  431.     {
  432.       while (! empty ())
  433.     {
  434.       tree_switch_case *t = remove_front ();
  435.       delete t;
  436.     }
  437.     }
  438.  
  439.   void eval (const octave_value& val);
  440.  
  441.   void accept (tree_walker& tw);
  442. };
  443.  
  444. #endif
  445.  
  446. /*
  447. ;;; Local Variables: ***
  448. ;;; mode: C++ ***
  449. ;;; End: ***
  450. */
  451.